package edu.unl.bsm.CoolingLoad.testing;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import edu.unl.bsm.CoolingLoad.Util.Utility;

public class ReadExcel {

  private String inputFile;

  public void setInputFile(String inputFile) {
    this.inputFile = inputFile;
  }

  public double[][] readT24_168(int hour, int weekDay) throws IOException  {
    File inputWorkbook = new File(inputFile);
    Workbook w;
    try {
      w = Workbook.getWorkbook(inputWorkbook);
      // Get the first sheet
      Sheet sheet = w.getSheet(6);
      // Loop over first 10 column and lines
      
      
		
		
		
		Cell[] dateTime = sheet.getColumn(0);
		
		Cell[] dayofweek = sheet.getColumn(2);
		
		//Cooling load at that hour point
		 Cell[] coolLoad_t_array = sheet.getColumn(4);
		 
		 //t-24
		 Cell[] coolLoad_t24_array = sheet.getColumn(5);
		 // t- 168
		 Cell[] coolLoad_t168_array = sheet.getColumn(6);
		 
		//Max Temperature
		 Cell[] Tmax_array = sheet.getColumn(7);
			
		//Min Temperature 
		 Cell[] Tmin_array = sheet.getColumn(8);
		 
		//Temperature at that hour point
		 Cell[] T_array = sheet.getColumn(9);
		 
		 Cell[] T24_array = sheet.getColumn(10);
		 
		 Cell[] T168_array = sheet.getColumn(11);
		 
		 Cell[] RHt_array = sheet.getColumn(12);
		 
		 Cell[] RHt24_array = sheet.getColumn(13);
		 
		 Cell[] RHt168_array = sheet.getColumn(14);
		 
		 int length = coolLoad_t_array.length-4;
//		 double[][] data = new double[length][12];
		 double[][] data = new double[5140][12];
		 List<double[]> list =  new ArrayList<double[]>();
		 
		 for(int i = 4+hour; i < 4468; i=i+24){
//		 for(int i = 4+hour; i < data.length; i=i+24){	 
			 if(weekDay == 1){
			 
				 if(Integer.parseInt(dayofweek[i].getContents())!=6&&Integer.parseInt(dayofweek[i].getContents())!=7){
					 data[i][0] = Double.parseDouble(coolLoad_t_array[i].getContents());
					 data[i][1] = 1d;
					 data[i][2] = Double.parseDouble(coolLoad_t24_array[i].getContents());
					 data[i][3] = Double.parseDouble(coolLoad_t168_array[i].getContents());
					 data[i][4] = Double.parseDouble(Tmax_array[i].getContents());
					 data[i][5] = Double.parseDouble(Tmin_array[i].getContents());
					 data[i][6] = Double.parseDouble(T_array[i].getContents());
					 data[i][7] = Double.parseDouble(T24_array[i].getContents());
					 data[i][8] = Double.parseDouble(T168_array[i].getContents());
					 data[i][9] = Double.parseDouble(RHt_array[i].getContents());
					 data[i][10] = Double.parseDouble(RHt24_array[i].getContents());
					 data[i][11] = Double.parseDouble(RHt168_array[i].getContents());
					 
					 //debug
					 System.out.print(dateTime[i].getContents() + ": ");
					 Utility.printArray(data[i]);
					 list.add(data[i]);
				 }
			 }else{
				 if(Integer.parseInt(dayofweek[i].getContents())>=6&&Integer.parseInt(dayofweek[i].getContents())<=7){
					 data[i][0] = Double.parseDouble(coolLoad_t_array[i].getContents());
					 data[i][1] = 1d;
					 data[i][2] = Double.parseDouble(coolLoad_t24_array[i].getContents());
					 data[i][3] = Double.parseDouble(coolLoad_t168_array[i].getContents());
					 data[i][4] = Double.parseDouble(Tmax_array[i].getContents());
					 data[i][5] = Double.parseDouble(Tmin_array[i].getContents());
					 data[i][6] = Double.parseDouble(T_array[i].getContents());
					 data[i][7] = Double.parseDouble(T24_array[i].getContents());
					 data[i][8] = Double.parseDouble(T168_array[i].getContents());
					 data[i][9] = Double.parseDouble(RHt_array[i].getContents());
					 data[i][10] = Double.parseDouble(RHt24_array[i].getContents());
					 data[i][11] = Double.parseDouble(RHt168_array[i].getContents());
					 
					 //debug
					 System.out.print(dateTime[i].getContents() + ": ");
					 Utility.printArray(data[i]);
					 list.add(data[i]);
				 }
			 }
//			 data[i][11] = 1d;
			 
		 }
		 
		 double[][] result = Utility.listToArray(list);
		 
//		 Utility.printArraySingleLine(result);
		return result;
      
    } catch (BiffException e) {
      e.printStackTrace();
    }
	return null;
	
  }
  
  public List<Object[]> readData() throws ParseException, IOException{
	  	File inputWorkbook = new File(inputFile);
	    Workbook w;
	    
	    try {
	        w = Workbook.getWorkbook(inputWorkbook);
	        // Get the first sheet
	        Sheet sheet = w.getSheet(0);
	        // Loop over first 10 column and lines
	        
	        
	  		
	  		
	  		
	  		Cell[] dateTime = sheet.getColumn(0);
	  		
//	  		Cell[] dayofweek = sheet.getColumn(2);
	  		
	  		//Cooling load at that hour point
	  		 Cell[] coolLoad_t_array = sheet.getColumn(5);
	  		 
//	  		 //t-24
//	  		 Cell[] coolLoad_t24_array = sheet.getColumn(5);
//	  		 // t- 168
//	  		 Cell[] coolLoad_t168_array = sheet.getColumn(6);
	  		 
	  		//Max Temperature
	  		 Cell[] Tmax_array = sheet.getColumn(1);
	  			
	  		//Min Temperature 
	  		 Cell[] Tmin_array = sheet.getColumn(2);
	  		 
	  		//Temperature at that hour point
	  		 Cell[] T_array = sheet.getColumn(3);
	  		 
//	  		 Cell[] T24_array = sheet.getColumn(10);
//	  		 
//	  		 Cell[] T168_array = sheet.getColumn(11);
	  		 
	  		 Cell[] RHt_array = sheet.getColumn(4);
	  		 
//	  		 Cell[] RHt24_array = sheet.getColumn(13);
//	  		 
//	  		 Cell[] RHt168_array = sheet.getColumn(14);
	  		 
	  		 int length = coolLoad_t_array.length-96;
	  		 Object[][] data = new Object[7300][6];
	  		 List<Object[]> list =  new ArrayList<Object[]>();
	  		 
	  		 for(int i = 1996; i < 6628; i++){
	  			 
	  			 
	  				 data[i][0] = Utility.convertToDate(dateTime[i].getContents());
	  				 data[i][1] = Float.parseFloat(Tmax_array[i].getContents());
	  				 data[i][2] = Float.parseFloat(Tmin_array[i].getContents());
	  				 data[i][3] = Float.parseFloat(T_array[i].getContents());
	  				 data[i][4] = Float.parseFloat(RHt_array[i].getContents());
	  				 data[i][5] = Float.parseFloat(coolLoad_t_array[i].getContents());
	  				
	  				 
	  				 
	  				 //debug
//	  				 System.out.print(dateTime[i].getContents() + ": ");
//	  				 Utility.printArray(data[i]);
	  				 list.add(data[i]);
	  			 
//	  			 data[i][11] = 1d;
	  			 
	  		 }
	  		 
	  		
	  		 
//	  		 Utility.printArraySingleLine(result);
	  		return list;
	        
	      } catch (BiffException e) {
	        e.printStackTrace();
	      }
	    return null;
  }
  
  
  //read next 24 hours data plug 168 buffer
  public List<Object[]> readForcastData() throws IOException, ParseException{
	  
	  File inputWorkbook = new File(inputFile);
	    Workbook w;
	    
	    try {
	        w = Workbook.getWorkbook(inputWorkbook);
	        // Get the first sheet
	        Sheet sheet = w.getSheet(0);
	        // Loop over first 10 column and lines
	        
	        
	  		
	  		
	  		
	  		Cell[] dateTime = sheet.getColumn(0);
	  		
//	  		Cell[] dayofweek = sheet.getColumn(2);
	  		
	  		//Cooling load at that hour point
	  		 Cell[] coolLoad_t_array = sheet.getColumn(5);
	  		 
//	  		 //t-24
//	  		 Cell[] coolLoad_t24_array = sheet.getColumn(5);
//	  		 // t- 168
//	  		 Cell[] coolLoad_t168_array = sheet.getColumn(6);
	  		 
	  		//Max Temperature
	  		 Cell[] Tmax_array = sheet.getColumn(1);
	  			
	  		//Min Temperature 
	  		 Cell[] Tmin_array = sheet.getColumn(2);
	  		 
	  		//Temperature at that hour point
	  		 Cell[] T_array = sheet.getColumn(3);
	  		 
//	  		 Cell[] T24_array = sheet.getColumn(10);
//	  		 
//	  		 Cell[] T168_array = sheet.getColumn(11);
	  		 
	  		 Cell[] RHt_array = sheet.getColumn(4);
	  		 
//	  		 Cell[] RHt24_array = sheet.getColumn(13);
//	  		 
//	  		 Cell[] RHt168_array = sheet.getColumn(14);
	  		 
	  		 int length = coolLoad_t_array.length-96;
	  		 Object[][] data = new Object[7300][6];
	  		 List<Object[]> list =  new ArrayList<Object[]>();
	  		 
	  		 for(int i = 6459; i < 6652; i++){
	  			 
	  			 
	  				 data[i][0] = Utility.convertToDate(dateTime[i].getContents());
	  				 data[i][1] = Float.parseFloat(Tmax_array[i].getContents());
	  				 data[i][2] = Float.parseFloat(Tmin_array[i].getContents());
	  				 data[i][3] = Float.parseFloat(T_array[i].getContents());
	  				 data[i][4] = Float.parseFloat(RHt_array[i].getContents());
	  				 data[i][5] = Float.parseFloat(coolLoad_t_array[i].getContents());
	  				
	  				 
	  				 
	  				 //debug
//	  				 System.out.print(dateTime[i].getContents() + ": ");
//	  				 Utility.printArray(data[i]);
	  				 list.add(data[i]);
	  			 
//	  			 data[i][11] = 1d;
	  			 
	  		 }
	  		 
	  		
	  		 
//	  		 Utility.printArraySingleLine(result);
	  		return list;
	        
	      } catch (BiffException e) {
	        e.printStackTrace();
	      }
	    return null;
	  
  }
  
  

  

} 